"""
检查数据库中的数据
"""
import asyncio
from tortoise import Tortoise
from tool.sessing import TORTOISE_ORM
from models.m import User, Novel, Chapter, NovelCategory, Admin


async def check_data():
    """检查数据库中的数据"""
    print("连接数据库...")
    await Tortoise.init(config=TORTOISE_ORM)
    print("[OK] 数据库连接成功\n")
    
    try:
        # 1. 查看管理员
        print("="*60)
        print("【管理员】")
        print("="*60)
        admins = await Admin.all().prefetch_related('user')
        for admin in admins:
            print(f"  用户名: {admin.username}")
            print(f"  关联用户: {admin.user.nickname} (ID: {admin.user.id})")
            print(f"  邮箱: {admin.email}")
            print(f"  角色: {admin.admin_role}")
            print()
        
        # 2. 查看作者
        print("="*60)
        print("【作者】")
        print("="*60)
        authors = await User.filter(role=2).all()
        for author in authors:
            novels_count = await Novel.filter(user=author).count()
            print(f"  姓名: {author.nickname}")
            print(f"  手机: {author.phone}")
            print(f"  ID: {author.id}")
            print(f"  简介: {author.bio}")
            print(f"  作品数: {novels_count}")
            print()
        
        # 3. 查看普通用户
        print("="*60)
        print("【普通用户】")
        print("="*60)
        normal_users = await User.filter(role=1).all()
        for user in normal_users[:5]:  # 只显示前5个
            print(f"  {user.nickname} (ID: {user.id}, 手机: {user.phone})")
        print(f"\n  共 {len(normal_users)} 个普通用户\n")
        
        # 4. 查看小说
        print("="*60)
        print("【小说列表】")
        print("="*60)
        novels = await Novel.all().prefetch_related('user', 'category')
        for novel in novels:
            chapter_count = await Chapter.filter(novel=novel).count()
            print(f"\n  【{novel.title}】")
            print(f"    ID: {novel.id}")
            print(f"    作者: {novel.user.nickname}")
            print(f"    分类: {novel.category.name}")
            print(f"    状态: {'连载中' if novel.status == 1 else '已完结'}")
            print(f"    审核状态: {'已通过' if novel.audit_status == 2 else '待审核'}")
            print(f"    字数: {novel.word_count}")
            print(f"    章节数: {chapter_count}")
            print(f"    浏览量: {novel.view_count}")
            print(f"    简介: {novel.description[:50]}...")
        
        # 5. 统计信息
        print("\n" + "="*60)
        print("【统计信息】")
        print("="*60)
        total_users = await User.all().count()
        total_admins = await Admin.all().count()
        total_authors = await User.filter(role=2).count()
        total_normal_users = await User.filter(role=1).count()
        total_novels = await Novel.all().count()
        total_chapters = await Chapter.all().count()
        total_categories = await NovelCategory.all().count()
        
        print(f"  用户总数: {total_users}")
        print(f"    - 管理员: {total_admins}")
        print(f"    - 作者: {total_authors}")
        print(f"    - 普通用户: {total_normal_users}")
        print(f"  小说总数: {total_novels}")
        print(f"  章节总数: {total_chapters}")
        print(f"  分类总数: {total_categories}")
        
    finally:
        await Tortoise.close_connections()


if __name__ == "__main__":
    asyncio.run(check_data())

